﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Encoding.Models
{
    /// <summary>
    /// 加密类
    /// </summary>
    internal class MyEncoder
    {
        /// <summary>
        /// 明文
        /// </summary>
        public string Plaintext { get; set; }

        /// <summary>
        /// 密文
        /// </summary>
        public string Cypher { get; set; }

        /// <summary>
        /// 密钥
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// 加密动作
        /// </summary>
        public void Encode()
        {
            //ToDo 加密过程
            for (int i = 0, j = 0; i < Plaintext.Length; i++, j++)
            {
                //让密钥循环出现
                if (j % Key.Length == 0)
                    j = 0;
                //如果变动后超过了26需要进行取余
                int num = (Tonum(Plaintext[i]) + Tonum(Key[j])) % 26;
                //得出密文
                Cypher += Convert.ToChar(num + 'a');
            }
        }
        /// <summary>
        /// 返回指定字符在字母表中的序号 从0开始
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private int Tonum(char ch)
        {
            return ch - 'a';
        }

    }

}
